//
//  modifyPassword.m
//  ShareNavigation
//
//  Created by 水木 傅 on 12-5-15.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ModifyPasswordViewController.h"
#import "SingletonUser.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"

@implementation ModifyPasswordViewController
@synthesize OriginPassWord;
@synthesize NewPassWord;
@synthesize AgainPassWord;
@synthesize UserID;
@synthesize ModifyButton;

-(void) viewDidLoad
{
    self.UserID = [SingletonUser shareManager].UserID;
}

- (void)viewDidUnload {
    [self setUserID:nil];
    [self setOriginPassWord:nil];
    [self setNewPassWord:nil];
    [self setAgainPassWord:nil];
    [self setModifyButton:nil];
    [super viewDidUnload];
}
- (IBAction)SendDataPress:(id)sender
{
    [self sendData];
}

- (IBAction)OriginPasswordDidEnd:(id)sender {
    [NewPassWord becomeFirstResponder];
}

- (IBAction)NewPasswordDidEnd:(id)sender {
    [AgainPassWord becomeFirstResponder];
}

- (IBAction)AgainPassWordDidEnd:(id)sender {
    if([self TwicePassWordtheSame])
    {
        [AgainPassWord resignFirstResponder];
    }
    else
    {
        NSString *msg = @"两次输入的密码不同或尚未输入密码，请重新输入。";
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"警告"
                              message:msg
                              delegate:self
                              cancelButtonTitle:@"返回"
                              otherButtonTitles:nil];
        alert.tag = 0;
        [alert show];
    }
}

- (IBAction)ChangePassWord:(id)sender {
    [self TwicePassWordtheSame];
}

-(BOOL)TwicePassWordtheSame
{
    if([self.NewPassWord.text isEqualToString:self.AgainPassWord.text] 
       && [self.NewPassWord.text length] != 0)
    {
        self.ModifyButton.enabled = TRUE;
        return TRUE;
    }
    else
    {
        self.ModifyButton.enabled = FALSE;
        return FALSE;
    }
}
//---  发送修改信息  ---
-(void) requestFinished:(ASIHTTPRequest *)request
{
    NSString *ResponseString = [request responseString];
    NSLog(@"%@",ResponseString);
    if([ResponseString isEqualToString:@"TRUE"])
    {
        UIAlertView *alert;
        ResponseString = @"修改密码成功";
        alert = [[UIAlertView alloc]
                 initWithTitle:@""
                 message:ResponseString
                 delegate:self
                 cancelButtonTitle:@"返回"
                 otherButtonTitles:nil];
        alert.tag = 1;
        [alert show];
    }
    else
    {
        UIAlertView *alert;
        alert = [[UIAlertView alloc]
                 initWithTitle:@"警告"
                 message:ResponseString
                 delegate:self
                 cancelButtonTitle:@"返回"
                 otherButtonTitles:nil];
        [alert setTag:2];
        [alert show];
    }
}

-(void) requestFailed :(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    NSLog(@"%@",[error localizedFailureReason]);
}

-(void)sendData
{
    NSURL *url = [NSURL URLWithString:@"http://localhost/~walker/ShareNavigationSVN/trunk/ShareNavigationPHP/ModifyPassword.php"];
    ASIFormDataRequest *request=[[ASIFormDataRequest alloc] initWithURL:url];
    [request setRequestMethod:@"POST"];
    [request setPostValue:self.UserID forKey:@"userID"];
    [request setPostValue:self.OriginPassWord.text forKey:@"oldPassword"];
    [request setPostValue:self.NewPassWord.text forKey:@"newPassWord"];
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(requestFinished:)];
    [request setDidFailSelector:@selector(requestFailed:)];
    [request setDefaultResponseEncoding:NSUTF8StringEncoding];
    //---  发送请求信息  ----
    [request startAsynchronous];

}

#pragma ---  重写tableview 的函数  ---
//---  重写函数，tableview中的行不可被选中  ---
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
            [OriginPassWord becomeFirstResponder];
            break;
        case 1:
            [NewPassWord becomeFirstResponder];
            break;
        case 2:
            [AgainPassWord becomeFirstResponder];
            break;
        default:
            break;
    }
    return nil;
}

#pragma ---  弹出框委托  ----
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag == 0)
    {
        //---  光标重新聚焦到确认的密码输入框  ---
        [AgainPassWord becomeFirstResponder];
    }
}
@end
